<template>
<div class="reg" >
    <el-card style="width: 500px;margin:50px auto">
      <el-form class="demo-ruleForm" :model="user" status-icon :rules="rules" ref="ruleForm"
               label-width="80px" show-message="true" inline-message="true" label-position="left">
        <el-form-item>
          <h1 style="float: left;font-size: 28px">立即注册</h1>
          <a style="float: right;
                                text-decoration: none;color: #409EFF;
                                position: relative;top: 20px"  href="login.html">
            已有账号?现在登录
          </a>
        </el-form-item>
        <el-form-item
            prop="email"
            label="邮箱"
            :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
        >
          <el-input v-model="user.email" ></el-input>
          <el-button type="primary" :disabled="isSend||isCountingDown" @click="sendCode">{{message}}</el-button>
        </el-form-item>
        <el-form-item label="验证码"  >
          <el-input type="text" v-model="user.emailCode" autocomplete="off" ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password" >
          <el-input type="password" v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass" >
          <el-input type="password" v-model="user.checkPass" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName" :rules="{required: true, message: '昵称不能为空', trigger: 'blur'}">
          <el-input placeholder="请输入昵称" v-model="user.nickName" @keydown.native.enter="reg()"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <el-button type="primary" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
</div>
</template>

<script>
export default {
  name: "RegView",
  data() {
    return {
      isSend:false,
      verificationCode: '',
      isCountingDown: false,
      countDownSeconds: 60,
      dynamicValidateForm: {
        domains: [{
          value: ''
        }],
        email: ''
      },
      ruleForm: {
        password: '',
        checkPass: '',
      },
      user: {
        password: '',
        checkPass: '',
        nickName:'',
        email:"",
        emailCode:""
      },

      rules: {
        nickname:[{ validator: validatePass, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' },
          {required:true,message:"请输入密码",trigger:'blue'},
          {min:4,max:15,message:'长度在4到15个字符',trigger:'blue'}],
        checkPass: [{ validator: validatePass2, trigger: 'blur' },
          {required:true,message:"请再次输入密码",trigger:'blue'},
          {min:4,max:15,message:'长度在4到15个字符',trigger:'blue'}],
        username:[{ validator: validatePass, trigger: 'blur' }],

      },
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
  },
  computed: {
    message() {
      if (this.isSend) {
        return '发送中...'
      }
      if (this.isCountingDown) {
        return `${this.countDownSeconds}秒后重新获取`
      }
      return '获取验证码'
    }
  },
  methods: {
    submitForm(formName) {
      console.log(123)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url= 'http://localhost:8080/v1/users/reg'
          console.log("url ="+url)
          let formData = this.qs.stringify(this.user);
          console.log(formData)
          this.axios.post(url,formData).then((response)=>{
            console.log(response.data)
            if(response.data.code==1){
              alert("注册成功！")
              location.href="/login"
            }else{
              this.$message.error(response.data.msg)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    sendCode() {
      this.isSend = true
      this.axios.post("http://localhost:8080/v1/emails/"+this.user.email+"/code").then(function (response) {
        if(response.data.code==1){
          // this.code=response.data.data;
        }
      })
      // 发送验证码的异步操作，这里使用setTimeout模拟异步操作
      setTimeout(() => {
        this.isSend = false
        this.isCountingDown = true
        this.countDownSeconds = 60
        this.startCountDown()
      }, 1000)
    },
    startCountDown() {
      if (this.countDownSeconds === 0) {
        this.isCountingDown = false
        return
      }
      this.countDownSeconds--
      setTimeout(() => {
        this.startCountDown()
      }, 1000)
    },
  },
}
</script>

<style scoped>
.reg{

}

</style>